| Conditions | 20 |
| Paths | 255 |
| Total Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like common.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Copyright © 2017 Tangdongxin |
||
| 156 | function parse (str, re) { |
||
| 157 | |||
| 158 | str = reconvert(str); |
||
| 159 | let match, val, tmp, i = 0; |
||
| 160 | const len = str.length; |
||
| 161 | try { |
||
| 162 | |||
| 163 | for (; match = re.exec(str);) { |
||
| 164 | |||
| 165 | val = match[0]; |
||
| 166 | if (val == '{' || val == '[') { |
||
| 167 | |||
| 168 | path.push(node); |
||
| 169 | node.appendChild(cache[val].cloneNode(true)); |
||
| 170 | node = node.lastChild.previousSibling; |
||
| 171 | node.len = 1; |
||
| 172 | node.start = re.lastIndex; |
||
| 173 | |||
| 174 | } else if ((val == '}' || val == ']') && node.len) { |
||
| 175 | |||
| 176 | if (node.childNodes.length) { |
||
| 177 | |||
| 178 | tmp = info.cloneNode(); |
||
| 179 | const content = node.len + ( |
||
| 180 | node.len == 1 ? |
||
| 181 | val == ']' ? ' item, ' : ' property, ' : |
||
| 182 | val == ']' ? ' items, ' : ' properties, ' |
||
| 183 | ) + units(re.lastIndex - node.start + 1); |
||
| 184 | |||
| 185 | if (hideDetails) { |
||
| 186 | |||
| 187 | tmp.setAttribute('title', content); |
||
| 188 | |||
| 189 | } else { |
||
| 190 | |||
| 191 | tmp.dataset.content = content; |
||
| 192 | |||
| 193 | } |
||
| 194 | |||
| 195 | if ((val = node.previousElementSibling) && val.className == KEY) { |
||
| 196 | |||
| 197 | tmp.dataset.key = reconvert(val.textContent.slice(1, -1).replace(/'/, '\\\'')); |
||
| 198 | |||
| 199 | } |
||
| 200 | node.parentNode.insertBefore(tmp, node); |
||
| 201 | |||
| 202 | } else { |
||
| 203 | |||
| 204 | node.parentNode.removeChild(node); |
||
| 205 | |||
| 206 | } |
||
| 207 | node = path.pop(); |
||
| 208 | |||
| 209 | } else if (val == ',') { |
||
| 210 | |||
| 211 | node.len += 1; |
||
| 212 | node.appendChild(comma.cloneNode(true)); |
||
| 213 | |||
| 214 | } else { |
||
| 215 | |||
| 216 | tmp = span.cloneNode(); |
||
| 217 | tmp.textContent = match[1] || val; |
||
| 218 | tmp.classList.add(match[3] ? KEY : match[1] ? STR : match[4] ? ERR : BOOL); |
||
| 219 | node.appendChild(tmp); |
||
| 220 | if (match[3]) { |
||
| 221 | |||
| 222 | node.appendChild(colon.cloneNode()); |
||
| 223 | |||
| 224 | } |
||
| 225 | |||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | document.title = ''; |
||
| 230 | JSON.parse(str); |
||
| 231 | |||
| 232 | } catch (e) { |
||
| 233 | |||
| 234 | dlog(e); |
||
| 235 | // TODO: find a better way to report error |
||
| 236 | |||
| 237 | } |
||
| 238 | |||
| 239 | } |
||
| 240 | |||
| 242 |